/*
 * Copyright 2011.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db.obj.vis.ui.util;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
import org.opu.db.obj.vis.ui.actions.AbstractAction;

/**
 *
 */
public class GUIUtils {

    public static final String CUT = "Cut";
    public static final String COPY = "Copy";
    public static final int DEFAULT_SPACE = 16;
    public static final int DEFAULT_SPACE_HALF = 8;
    public static final String PASTE = "Paste";

    public interface ActionListener {

        public void actionPerformed(ActionEvent e) throws Exception;

    }

    // Suppresses default constructor, ensuring non-instantiability.
    private GUIUtils() {
    }

    public static <T extends JTextComponent> T addActions(final T textComponent) {
        if (textComponent == null) {
            return null;
        }

        final AbstractAction cut = getAction(CUT, new ActionListener() {

            @Override public void actionPerformed(ActionEvent e) {
                textComponent.cut();
            }
        });

        AbstractAction copy = getAction(COPY, new ActionListener() {

            @Override public void actionPerformed(ActionEvent e) {
                textComponent.copy();
            }
        });

        final AbstractAction paste = getAction(PASTE, new ActionListener() {

            @Override public void actionPerformed(ActionEvent e) {
                textComponent.paste();
            }
        });
        
        final JPopupMenu menu = new JPopupMenu();
        menu.add(cut);
        menu.add(copy);
        menu.add(paste);

        textComponent.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                 if (SwingUtilities.isRightMouseButton(evt)) {
                    boolean edit = textComponent.isEditable()
                            && textComponent.isEnabled();
                    cut.setEnabled(edit);
                    paste.setEnabled(edit);
                    menu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        });

        return textComponent;
    }
    
    public static AbstractAction getAction(final String name, 
    		final ActionListener run) {
    	return getAction(name, run, false);
    }

    public static AbstractAction getAction(final String name, 
    		final ActionListener run, boolean noIcon) {
        AbstractAction action = new AbstractAction(true) {

			private static final long serialVersionUID = 8435429202561270166L;

			@Override
            public String getName() {
                return name;
            }

            @Override
            protected void setIcon() {
                super.setIcon();
                putValue(LARGE_ICON_KEY, null);
            }

            @Override
            public void action(ActionEvent e) throws Exception{
                run.actionPerformed(e);
                //onActionFinish();
            }
        };
        
        if (noIcon){
        	action.putValue(AbstractAction.NAME, null);
        }
        
        return action;
    }

    public static JComponent hBorder(JComponent comp) {
        return hBorder(comp, DEFAULT_SPACE, DEFAULT_SPACE);
    }

    public static JComponent hBorder(JComponent comp, int i1, int i2){
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalStrut(i1));
        box.add(comp);
        box.add(Box.createHorizontalStrut(i2));
        return box;
    }

    public static JComponent hCenter(JComponent comp){
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        box.add(comp);
        box.add(Box.createHorizontalGlue());
        return box;
    }

    public static JComponent vBorder(JComponent comp) {
        return vBorder(comp, DEFAULT_SPACE, DEFAULT_SPACE);
    }

    public static JComponent vBorder(JComponent comp, int i1, int i2){
        Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(i1));
        box.add(comp);
        box.add(Box.createVerticalStrut(i2));
        return box;
    }

    public static JComponent vCenter(JComponent comp){
        Box box = Box.createVerticalBox();
        box.add(Box.createVerticalGlue());
        box.add(comp);
        box.add(Box.createVerticalGlue());
        return box;
    }

    public static void setSize(JDialog dg, int width){
        dg.pack();
        Dimension size = dg.getSize();
        dg.setSize(size.width > width ? size.width : width, size.height);
    }
}
